var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

var pool = require('../pool.js');

router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());

var obj = {uname:'账号',upwd:'密码',email:'邮箱',phone:'手机',user_name:'用户名',gender:'性别'};
var code = {uname:401,upwd:402,email:403,phone:404,user_name:405,gender:406};


function checkNull(req_body){
	var arr = [];
	for (var k in req_body)
	{
		if(!req_body[k] && !(typeof(obj[k]) == 'undefined')){
			console.log(req_body[k]);
			var msg = {code:code[k],msg:obj[k] + '不能为空'};
			arr.push(msg);
		}
	}
	return arr;
}

function checkStyle(req_body){
	var arr_check = [];
	if(!(/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(req_body.email))){
		msg = '邮箱格式不正确';
		arr_check.push({code:407,msg:msg});
	}
	if(!(/^1[3-9]\d{9}$/.test(req_body.phone))){
		msg = '手机格式不正确';
		arr_check.push({code:408,msg:msg});
	}
	return arr_check;
}

router.put('/', function(req, res, next) {
	var arr_null = checkNull(req.body);
	
	if(arr_null.length != 0){
		res.send(arr_null);
	}
	else{
		var arr_check = checkStyle(req.body);
		if(arr_check.length != 0){
			res.send(arr_check);
		}
		else{
			var sql = 'update xz_user set ? where uid = ?';
			pool.query(sql,[req.body,req.body.uid],function (err, result) {
			if(err){
				next(err);
				return;
			}
			console.log(result);
			if(result.affectedRows === 0){
				res.send({code:201,msg:'修改失败'});
			}
			else{
				res.send({code:200,msg:'修改成功'});
			}
			});
		}
	}
});

router.post('/reg', function(req, res, next) {
	var arr_null = checkNull(req.body);
	var arr_check = [];
	if(arr_null.length != 0){
		res.send(arr_null);
	}
	else{
		var arr_check = checkStyle(req.body);
		if(arr_check.length != 0){
			res.send(arr_check);
		}
		else{
			var sql = 'INSERT INTO xz_user set ?';
			pool.query(sql,[req.body],function (err, result) {
			if(err){
				next(err);
				return;
			}
			console.log(result);
			res.send('注册成功,编号:' + result.insertId);
			});
		}
	}
});

router.post('/login',(req,res,next) => {
	var arr_null = checkNull(req.body);
	if(arr_null.length != 0){
		res.send(arr_null);
	}
	else{
		var sql = 'select upwd from xz_user where uname = ?';
		pool.query(sql,[req.body.uname],function (err, result) {
		if(err){
			next(err);
			return;
		}
		if(result.length == 0){
			res.send({code:409,msg:'用户名不存在'});
		}
		else{
			console.log(req.body.upwd);
			console.log(result[0].upwd);
			if(req.body.upwd === result[0].upwd){
				res.send({code:200,msg:'登录成功'});
			}
			else{
				res.send({code:410,msg:'密码不正确'});
			}
		}
		});
	}
});

router.delete('/:uid', function (req, res, next) {
	var  sql = 'DELETE FROM xz_user where uid=?';
	pool.query(sql,[req.params.uid],function (err, result) {
		if(err){
			next(err);
			return;
		}
		if(result.affectedRows === 0){
			console.log(result);
			res.send({code:201,msg:'删除失败'});
		}
		else{
			console.log(result);
			res.send({code:200,msg:'删除成功'});
		}
	});
});

router.get('/:uid', function (req, res, next) {
	var  sql = 'SELECT * FROM xz_user where uid=?';
	pool.query(sql,[req.params.uid],function (err, result) {
		if(err){
			next(err);
			return;
		}
	    if(result.length === 0){
			console.log(result);
			res.send({code:201,msg:'该用户不存在'});
		}
		else{
			console.log(result);
			res.send({code:200,msg:'查询成功',data:result});
		}
	});
});

router.get('/all/:limit', function (req, res, next) {
	var  sql = 'SELECT * FROM xz_user limit ?,10';
	pool.query(sql,[(req.params.limit - 1) * 10],function (err, result) {
		if(err){
			next(err);
			return;
		}
	    if(result.length === 0){
			console.log(result);
			res.send({code:201,msg:'当前无用户'});
		}
		else{
			console.log(result);
			res.send({code:200,msg:'查询成功第' + req.params.limit + '页',data:result});
		}
	});
});

module.exports = router;